# -*- coding: utf-8 -*-
import re

import noval.util.strutils as strutils
from noval.editor.code import CodeView
from noval.python.parser.utils import py_sorted
from noval import GetApp

from autocompletion.indexing import BackgroundIndexer

indexer=BackgroundIndexer(False)
camelCaseRegex=re.compile(r'[\w|\d]([A-Z])')# 识别驼峰命名的正则表达式
underLineCaseRegex=re.compile(r'_(\w)')# 识别下划线命名方法的正则表达式
def ifMatchCamelCase(word='',hint=''):
    '''
    匹配形如‘isValidStudentNumber’这类的驼峰命名法。
    '''
    global camelCaseRegex
    if len(word)>=1:
        sub=re.findall(camelCaseRegex,word)

        s=word[0]+''.join(sub)
        if s.lower().startswith(hint):
            return True
    return False
    
def ifMatchUnderlineCase(word='',hint=''):
    '''
    匹配形如‘is_valid_student_number’这类的下划线命名法。
    '''
    global underLineCaseRegex
    if len(word)>=1:
        sub=re.findall(underLineCaseRegex,word)
        s=word[0]+''.join(sub)
        if s.lower().startswith(hint):
            return True
    return False
    
def filterWords(wordList,hint):
    '''
    提取下划线或者驼峰命名的字符。
    '''
    resultList=[]
    for word in wordList:
        if word.lower().startswith(hint):
            resultList.append(word)
        elif ifMatchCamelCase(word,hint):
            resultList.append(word)
        elif ifMatchUnderlineCase(word,hint):
            resultList.append(word)
        else:
            continue
    return resultList
            

def GetAutoCompleteKeywordList(self, context, hint,line):            
    """
     Replace this method with Editor specific keywords
    """
    kw = self.GetAutoCompleteKeywords(line)
    kw2= indexer.getLocalVarList(line)
    kw+=kw2
    if hasattr(self,'promptLex'):
        kw+=self.promptLex
    
    if not kw:
        return
        
    kw = list(set(kw))
    if hint and len(hint):
        lowerHint = hint.lower()
        filterkw=filterWords(kw,lowerHint)
        kw = filterkw
    if hint:
        replaceLen = len(hint)
    else:
        replaceLen = 0

    kw = py_sorted(kw,cmp_func=strutils.caseInsensitiveCompare)
    return kw, replaceLen
    
def lexParse(self):
    '''
    通过词法分析导出初步的提示。但是这种提示并不是智能的，可能存在错误。
    '''
    
    self.promptLex = []
    current_editor = GetApp().MainFrame.GetNotebook().get_current_editor()
    if current_editor is not None:          
        current_docview = current_editor.GetView()    
        if current_docview:  
            input_psd=current_docview.GetValue()#获取当前窗口的文本内容
    if not input_psd:
        self.promptLex = []
        
    test_str = re.findall(r"\W",input_psd)
    s3 = re.sub(r'\W', ' ', input_psd, count=0, flags=0)
    l = s3.split(' ')
    for i,k in enumerate(l):
        l[i] = k.strip()
    self.promptLex =  list(set(l))

CodeView.GetAutoCompleteKeywordList=GetAutoCompleteKeywordList# 重写所需的方法，在这里进行补全。
CodeView.lexParse = lexParse
CodeView.promptLex = []

if __name__=='__main__':
    '''
    以下内容为测试。
    '''
    a=ifMatchUnderlineCase('jiang_jie_shi','jj')
    b=ifMatchCamelCase('suchAsaGoodDream','sa')
    